#include "stdafx.h"
#include "ui/ui_base/editable_list_ctrl.h"

IMPLEMENT_DYNAMIC(CEditableListCtrl, CListCtrl)

CEditableListCtrl::CEditableListCtrl()
{

}

CEditableListCtrl::~CEditableListCtrl()
{

}

BOOL
CEditableListCtrl::CanEdit(int nRow, int nCol)
{
    return TRUE;
}

BOOL CEditableListCtrl::OnRequestEdit(int nRow, int nCol, CRect oEditRect)
{
    return TRUE;
}

BOOL CEditableListCtrl::OnRequestDelete(int nRow)
{
    return FALSE;
}

BOOL CEditableListCtrl::GetSubItemRect(int iItem, int iSubItem, int nArea, CRect& ref)
{
    BOOL bRet = CListCtrl::GetSubItemRect(iItem, iSubItem, nArea, ref);
    if(LVIR_BOUNDS != nArea || 0 != iSubItem) {
        return bRet;
    }
    
    ref.right = ref.left + GetColumnWidth(0);

    return bRet;
}

BEGIN_MESSAGE_MAP(CEditableListCtrl, CListCtrl)
    ON_WM_LBUTTONDBLCLK()
    ON_WM_KEYUP()
END_MESSAGE_MAP()

void CEditableListCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    LVHITTESTINFO oHitTestInfo = {0};
    oHitTestInfo.pt = point;
    SubItemHitTest(&oHitTestInfo);

    if(CanEdit(oHitTestInfo.iItem, oHitTestInfo.iSubItem)) {
        CRect oItemRect;
        GetSubItemRect(oHitTestInfo.iItem, oHitTestInfo.iSubItem, LVIR_BOUNDS, oItemRect);
        OnRequestEdit(oHitTestInfo.iItem, oHitTestInfo.iSubItem, oItemRect);
    }

    CListCtrl::OnLButtonDblClk(nFlags, point);
}

void CEditableListCtrl::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // TODO: Add your message handler code here and/or call default
    if(nChar == VK_DELETE) {
        OnRequestDelete(GetSelectionMark());
    }

    CListCtrl::OnKeyUp(nChar, nRepCnt, nFlags);
}
